package com.javaweb.dao.impl;

import com.javaweb.aop.NPECatch;
import com.javaweb.common.StringUtils;
import com.javaweb.dao.PermDao;
import com.javaweb.model.Permission;
import com.javaweb.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/24 22:43
 */

/**
 * todo: 实现全部如下方法
 */
@Slf4j
@Repository
@NPECatch
public class PermDaoImpl implements PermDao {

    @Autowired
    private Connection conn ;
    @Override
    public Integer insert(Permission perm) {
        String sql = "INSERT INTO tb_permission (name,url) VALUES" +
                "(?, ?)" ;
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setString(1, perm.getName()) ;
            ps.setString(2, perm.getUrl()) ;

            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert  error: " + e.getMessage()) ;
        }
        return 0 ;

    }

    @Override
    public Integer insertRolePerm(int roleId, int permId) {
        String sql = "INSERT INTO tb_role_per (role_id,per_id) VALUES" +
                "(?, ?)" ;
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setInt(1, roleId); ;
            ps.setInt(2, permId); ;

            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert  error: " + e.getMessage()) ;
        }
        return 0 ;


    }

    @Override
    public Permission selectOne(String name, String url) {
        String sql = "SELECT * FROM tb_permission WHERE name=? and url=?" ;
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setString(1, name) ;
            ps.setString(2, url) ;
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(name)
                        .setUrl(url);


                return permission ;
            }
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return null;


    }

    @Override
    public Permission selectOneById(int permId) {

        String sql = "SELECT * FROM tb_permission WHERE per_id=?" ;

        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setInt(1, permId); ;

            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(permId)
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));

return permission;

            }

        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return null;

    }

    @Override
    public List<Permission> selectPermsByRoleName(String roleName) {

        String sql = "SELECT p.* FROM tb_permission p " +
                " JOIN tb_role_per rp ON p.per_id = rp.per_id" +
                " JOIN tb_role r ON r.role_id = rp.role_id" +
                "  WHERE r.name = ?" ;
        List<Permission> permissionList = new ArrayList<>() ;
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setString(1, roleName) ;

            ResultSet rs = ps.executeQuery() ;
            while (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));
                permissionList.add(permission);


            }return permissionList;
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return permissionList;
    }

    @Override
    public List<Permission> selectPermsByRoleId(int roleId){

        String sql = "SELECT p.* FROM tb_permission p " +
                " JOIN tb_role_per rp ON p.per_id = rp.per_id" +
                " JOIN tb_role r ON r.role_id = rp.role_id" +
                "  WHERE r.role_id = ?" ;
        List<Permission> permissionList = new ArrayList<>() ;
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setInt(1, roleId); ;

            ResultSet rs = ps.executeQuery() ;
            while (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));
                permissionList.add(permission);


            }return permissionList;
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return permissionList;
    }

    @Override
    public Integer deleteById(int permId) {
        String sql = "delete from tb_permission where per_id=?" ;
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setInt(1, permId); ;


            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert  error: " + e.getMessage()) ;
        }
        return 0 ;

    }
}
